// ucenter_personal.js
const app = getApp(), core = require('../../utils/core');
const ucenterBehavior = require('ucenter_behavior');
Component({
  behaviors: [ucenterBehavior],
  options: {
    styleIsolation: 'apply-shared'
  },

  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    lastRefreshTime: 0,
    isIPhoneX: null
  },

  lifetimes: {
    // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
    attached: function () {
      app.eventBus.on(app.Events.InterviewCommentAdd, this.eventOnInterviewCommentAdd, this);
      let isIPhoneX = this.data.isIPhoneX;
      return new Promise((resolve, reject) => {
        if (isIPhoneX !== null) {
          resolve(isIPhoneX);
        }
        else {
          wx.getSystemInfo({
            success: ({ model, screenHeight }) => {
              const iphoneX = /iphone x/i.test(model);
              const iphoneNew = /iPhone11/i.test(model);
              isIPhoneX = iphoneX || iphoneNew;
              resolve(isIPhoneX);
              this.setData({
                isIPhoneX: isIPhoneX
              })
              console.log(this.data.isIPhoneX)
            },
            fail: reject
          });
        }
      });
    },
    moved: function () { },
    detached: function () {
      app.eventBus.off(app.Events.InterviewCommentAdd, this.eventOnInterviewCommentAdd);
    },
  },

  pageLifetimes: {
    // 组件所在页面的生命周期函数
    show: function () {
      let now = new Date().getTime();
      if(now > this.data.lastRefreshTime + 60000*5) {
        this.refresh(true);
      }
    },
    hide: function () { },
    resize: function () { },
  },

  /**
   * 组件的方法列表
   */
  methods: {
    refresh: function(showLoading=true) {
      let component = this;
      core.get({
        url: '/xilujob.user/info',
        loading: showLoading,
        success(ret, data) {
          console.log(ret)
          component.setData({lastRefreshTime: new Date().getTime()});
          component.setData({
            summary: ret.data.summary
          });
          app.setUserinfo(ret.data.userinfo);
          component._createCanvas('one', 60 / 2, '#FF770A', '完成', ret.data.summary.info_complete, 100);
        }
      });
      this.setData({lastRefreshTime: new Date().getTime()})
    },
    eventOnInterviewCommentAdd: function (data) {
      this.setData({'summary.not_comment_count': data.count});
    }
  },

});
